package com.alibaba.cirtus.ots.application.adapter.ability.facade.serviceV2.crm;

import com.alibaba.cirtus.ots.application.adapter.ability.model.request.ContractMonthlyReq;
import com.alibaba.cirtus.ots.application.adapter.ability.utils.HttpTokenUtil;
import com.alibaba.cirtus.ots.application.adapter.ability.utils.HttpUtils;
import com.alibaba.citrus.ots.common.annotation.ProcessInvoker;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.lattice2.epoch.sdk.model.Result;
import com.epoch.app.otsapplicationadaptersdk.contants.OtsApplicationAdapterSdkConstants;
import com.epoch.app.otsapplicationadaptersdk.mncrmifcontract.service.MnCrmIfContractService;
import com.epoch.app.otsapplicationadaptersdk.model.dto.CrmContractMonthlyDTO;
import com.epoch.app.otsapplicationadaptersdk.model.dto.CrmContractMonthlyResp;
import com.epoch.app.otsapplicationadaptersdk.model.dto.IntegrationContractRequest;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Component;

import java.util.List;
import java.util.Map;

/**
 * @author
 * @Description sap请求中台同步合同量
 * @date 2024/4/26
 */
@Component
public class MnCrmIfContractServiceImpl implements MnCrmIfContractService {
    private static final Logger log = LoggerFactory.getLogger(MnCrmIfContractServiceImpl.class);
    private static final String URL = "/api/omni_application_adapter_sdk/crm_create_contract_monthly";
    private static final String OPERATOR_CODE = "SYSTEM";

    @Autowired
    private HttpUtils httpUtils;

    @Override
    @ProcessInvoker(remark = "MnCrmIfContractService", errorCode = "OTS-02-007-00-16-999", printLog = true)
    public Result<List<CrmContractMonthlyResp>> integrationContractMonthly(IntegrationContractRequest integrationContractRequest) {
        Result<List<CrmContractMonthlyResp>> result = new Result<>();
        try {
            Map<String, String> header = HttpTokenUtil.generateHttpHeader();
            String messageId = integrationContractRequest.getMessageHeader().getMessageId();
            List<CrmContractMonthlyDTO> reqList = integrationContractRequest.getItems();
            if (StringUtils.isNotBlank(messageId) && CollectionUtils.isNotEmpty(reqList)) {
                ContractMonthlyReq body = new ContractMonthlyReq();
                body.setRequestId(messageId);
                body.setReqList(reqList);
                body.setOperatorCode(OPERATOR_CODE);
                String url = OtsApplicationAdapterSdkConstants.urlPrefix() + URL;
                ResponseEntity<Result> resEntity = httpUtils.post(url, header, body, Result.class);
                log.info("integrationContractMonthly_resEntity:{}", JSONObject.toJSONString(resEntity));
                if (resEntity != null && resEntity.getBody() != null) {
                    List<CrmContractMonthlyResp> resList = (List<CrmContractMonthlyResp>) resEntity.getBody().getResult();
                    result.setResult(resList);
                    result.setTotal(resEntity.getBody().getTotal());
                    result.setSuccess(resEntity.getBody().isSuccess());
                }
            }else{
                result.setMessage("同步数据为空");
            }
        } catch (Exception e) {
            log.error("integrationContractMonthly_error", e);
            e.printStackTrace();
        }
        return result;
    }

}
